﻿using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using MSH.Common.DAO;
using WP7Client.Helpers;
using WP7Client.ServerManager;
using System;
using MSH.Common.Classes;
using GalaSoft.MvvmLight.Messaging;
using System.Windows;

namespace WP7Client.Controller
{
    public class ActivationController : BaseController
    {
        #region Members

        private String _ActivationCode;

        #endregion

        #region Get&Set

        public ICommand ActivateCommand 
        { 
            get; 
            internal set; 
        }

        public String ActivationCode
        {
            get { return _ActivationCode; }
            set { _ActivationCode = value; }
        }

        #endregion

        public ActivationController()
        {
            ActivateCommand = new RelayCommand(ActivateExecute);
        }

        /// <summary>
        /// Activeren van een nieuw account
        /// </summary>
        private void ActivateExecute()
        {
            if (String.IsNullOrEmpty(ActivationCode))
            {
                GlobalHelper.ShowMessage("Error", "Activationcode is required.");
                return;
            }

            HttpGet activateHttpGet = new HttpGet(String.Format(GlobalHelper.Url + "Activate/{0}/{1}/{2}", UserHelper.Username, UserHelper.Password, ActivationCode), typeof(ActivationResultEnum));
            activateHttpGet.FinishedGet += activateHttpGet_FinishedGet;
        }

        void activateHttpGet_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get == null || get.resultObject == null)
                    return; //Eventueel foutmelding

                ActivationResultEnum result = (ActivationResultEnum)get.resultObject;

                switch (result)
                {
                    case ActivationResultEnum.AlreadyActivated:
                        GlobalHelper.ShowMessage("Error", "Your account is already activated.");
                        break;
                    case ActivationResultEnum.Invalid:
                        GlobalHelper.ShowMessage("Error", "Your activationcode is invalid.");
                        break;
                    case ActivationResultEnum.Success:
                        ControllerHelper.LoginController.LoginExecute();
                        break;
                }
            }
            finally
            {
                Loading = false;
            }
        }  
    }
}
